package humynn.engine
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	
	/**
	 * A class used to manage the controls and entities for
	 * a part of the game.
	 */
	public class Screen
	{
		private var _bg:String;
		private var _loc:Point;
		private var _controls:Array;
		private var _entities:Array;
		
		public var blocksUpdate:Boolean;
		public var visible:Boolean;
		
		public function Screen()
		{
			_loc = new Point(0, 0);
			_controls = new Array();
			_entities = new Array();
			
			blocksUpdate = false;
			visible = true;
		}

		/**
		 * The screen's background image.
		 * 
		 * @param value The new background image.
		 */
		public function set background(value:String):void
		{
			_bg = value;
		}
		
		/**
		 * The screen's location in relation to the stage.
		 * 
		 * @returns The location of the screen.
		 */
		public function get loc():Point
		{
			return _loc;
		}
		
		/**
		 * The screen's location in relation to the stage.
		 * 
		 * @param value The new location of the screen.
		 */		
		public function set loc(value:Point):void
		{
			_loc = value;
		}
		
		/**
		 * Adds a control to the screen.
		 * 
		 * @param control The control to add.
		 */
		public function addControl(control:Control):void
		{
			if(control == null)
			{
				trace("control can not be null");
				return;
			}
			
			_controls.push(control);
		}
		
		/**
		 * Adds an entity to the screen
		 * 
		 * @param entity The entity to add.
		 */
		public function addEntity(entity:Entity):void
		{
			if(entity == null)
			{
				trace("null entity");
				return;
			}
			
			_entities.push(entity);
		}

		/**
		 * Update the various parts of the screen with the given input.
		 * 
		 * @param input The input from the user.
		 */
		public function update(input:Input):void
		{
			for each(var control:Control in _controls)
			{
				control.update(input);
			}
			
			for each(var entity:Entity in _entities)
			{
				entity.update(input);
			}
		}
		
		/**
		 * Draws the screen to the renderer.
		 * 
		 * @param renderer The renderer to draw to.
		 */
		public function draw(renderer:Renderer):void
		{
			if(_bg != null)
			{
				renderer.draw(_bg, 0, 0, loc.x, loc.y);
			}
			
			for each(var entity:Entity in _entities)
			{
				if(entity.visible)
				{
					entity.draw(renderer);
				}
			}
			
			for each(var control:Control in _controls)
			{
				control.draw(renderer);
			}
		}
	}
}